package net.oschina.bilbodai.common.retrofitor.support;

import net.oschina.bilbodai.common.retrofitor.util.Utils;
import retrofit2.Call;
import retrofit2.CallAdapter;
import retrofit2.Retrofit;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

/**
 * @author daimao  Date: 16/4/14 Time: 下午7:39
 * @version $Id$
 */
public class FooAdaterFactory extends CallAdapter.Factory {
    @Override
    public CallAdapter<?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
        if (Utils.getRawType(returnType) != Foo.class) {
            return null;
        }
        return new CallAdapter<Foo>() {
            @Override
            public Type responseType() {
                return Foo.class;
            }

            @Override
            public <R> Foo adapt(Call<R> call) {
                try {
                    return (Foo) call.execute().body();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        };
    }
}